/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.imagepipeline.filter;

import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.media.image.PixelMap;
import ohos.media.image.common.AlphaType;

/**
 * Not-in-place rounding image algorithm using {@link Canvas} that requires an Android version >=
 * 3.1. If image quality is preferred above performance, this algorithm performs anti-aliasing and
 * will generate better looking images, otherwise clients that look for better performances should
 * use NativeRoundingFilter.
 */
public final class XferRoundFilter {

    private XferRoundFilter() {
    }

    public static void xferRoundBitmap(PixelMap output, PixelMap source, boolean enableAntiAliasing) {
        if (output == null || source == null) throw new NullPointerException();
        //output.setHasAlpha(true);
        output.setAlphaType(AlphaType.OPAQUE);
        Paint circlePaint;
        Paint xfermodePaint;
        if (enableAntiAliasing) {
            circlePaint = new Paint();
            circlePaint.setAntiAlias(true);
            xfermodePaint = new Paint();
            xfermodePaint.setAntiAlias(true);
        } else {
            circlePaint = new Paint();
            xfermodePaint = new Paint();
        }

        circlePaint.setColor(Color.BLACK);
        //xfermodePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        xfermodePaint.setBlendMode(BlendMode.SRC_IN);

        Canvas canvas = new Canvas(new Texture(output));
        float xCenter = source.getImageInfo().size.width / 2f;
        float yCenter = source.getImageInfo().size.height / 2f;
        canvas.drawCircle(xCenter, yCenter, Math.min(xCenter, yCenter), circlePaint);
        canvas.drawPixelMapHolder(new PixelMapHolder(source),0,0,xfermodePaint);
    }

    public static boolean canUseXferRoundFilter() {
        //return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1;
        return true;
    }
}
